package com.chat.websocket.util.baidu;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * i
 * 2024 \ 01 \ 22
 */

@Slf4j
@Component
@ConfigurationProperties(prefix = "baidu.qianfan")
public class QianFanUtil {


    private String apiKey;

    private String secretKey;

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public void getAccessToken() {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token?client_id=" + apiKey + "&client_secret=" + secretKey + "&grant_type=client_credentials")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();
        try {
            Response response = HTTP_CLIENT.newCall(request).execute();
            String responseMessage = response.body().string();
            log.info("获取accessToken成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
